﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{
    public partial class QuanLyPhong : Form
    {
        public QuanLyPhong()
        {
            InitializeComponent();
        }
        public void layDSPhong()
        {
            GridDSPhong.Rows.Clear();

           /* if (cbb_LoaiPhong.SelectedIndex >= 0)
            {
                LoaiPhongDTO loaiPhongDuocChon = (LoaiPhongDTO)cbb_LoaiPhong.Items[cbb_LoaiPhong.SelectedIndex];
                List<PhongDTO> dsPhong = PhongBUS.layDSPhongTheoMaLoaiPhong(loaiPhongDuocChon.MaLoaiPhong);

                for (int i = 0; i < dsPhong.Count; i++)
                {
                    GridDSPhong.Rows.Add(dsPhong[i].MaPhong, dsPhong[i].TenPhong, dsPhong[i].LoaiPhong.TenLoaiPhong, dsPhong[i].LoaiPhong.DonGia, dsPhong[i].GhiChu);
                    GridDSPhong.Rows[GridDSPhong.RowCount - 1].Tag = dsPhong[i];
                }
            } */
            
            List<LoaiPhongDTO> dsLoaiPhong = LoaiPhongBUS.layDSLoaiPhong();
            for(int k=0; k<dsLoaiPhong.Count;k++)
            {
                List<PhongDTO> dsPhong = PhongBUS.layDSPhongTheoMaLoaiPhong(dsLoaiPhong[k].MaLoaiPhong);
                for (int i = 0; i < dsPhong.Count; i++)
                {
                    GridDSPhong.Rows.Add(dsPhong[i].MaPhong, dsPhong[i].TenPhong, dsPhong[i].LoaiPhong.TenLoaiPhong, dsPhong[i].LoaiPhong.DonGia, dsPhong[i].GhiChu,dsPhong[i].SoKhachHienCo);
                    GridDSPhong.Rows[GridDSPhong.RowCount - 1].Tag = dsPhong[i];
                    
                }
            }           
        }

        private void LayDSLoaiPhong()
        {
            List<LoaiPhongDTO> dsLoaiPhong = LoaiPhongBUS.layDSLoaiPhong();
            cbb_LoaiPhong.DataSource  = dsLoaiPhong;
            cbb_LoaiPhong.DisplayMember = "TenLoaiPhong";
        }

        private void cbb_LoaiPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            //layDSPhong();
            LoaiPhongDTO loaiPhongDuocChon = (LoaiPhongDTO)cbb_LoaiPhong.Items[cbb_LoaiPhong.SelectedIndex];
            tb_donGia.Text = loaiPhongDuocChon.DonGia.ToString();
        }

        public  void Form1_Load(object sender, EventArgs e)
        {
            LayDSLoaiPhong();
            layDSPhong();
        }


        private void GridDSPhong_SelectionChanged_1(object sender, EventArgs e)
        {
            if (GridDSPhong.CurrentRow.Tag != null)
            {
                PhongDTO phongDuocChon = (PhongDTO)GridDSPhong.CurrentRow.Tag;
               tb_tenPhong.Text = phongDuocChon.TenPhong;
                tb_donGia.Text = phongDuocChon.LoaiPhong.DonGia.ToString();
                tb_ghiChu.Text = phongDuocChon.GhiChu;
                cbb_LoaiPhong.Text= phongDuocChon.LoaiPhong.TenLoaiPhong;
                
            }
        }

        private void btn_themMoi_Click(object sender, EventArgs e)
        {

            PhongDTO phongThemVao = new PhongDTO();
            LoaiPhongDTO loaiPhongThemVao = (LoaiPhongDTO)cbb_LoaiPhong.Items[cbb_LoaiPhong.SelectedIndex];

            if ((tb_tenPhong.Text.Trim() != null) && (cbb_LoaiPhong.SelectedIndex >= 0))
            {
                try
                {
                    phongThemVao.TenPhong = tb_tenPhong.Text.Trim();
                    phongThemVao.GhiChu = tb_ghiChu.Text.Trim();
                    long maLoaiPhong = loaiPhongThemVao.MaLoaiPhong;
                    bool ketQua = PhongBUS.them(phongThemVao, maLoaiPhong);
                    if (ketQua == false)
                        throw new Exception();

                    MessageBox.Show("Thêm phòng thành công !");
                    layDSPhong();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Thêm phòng thất bại !");
                }
            }          
        }

        private void btn_capNhat_Click(object sender, EventArgs e)
        {
            if (GridDSPhong.CurrentRow.Tag != null)
            {
                try
                {
                    PhongDTO phongDuocChon = (PhongDTO)GridDSPhong.CurrentRow.Tag;
                    phongDuocChon.TenPhong = tb_tenPhong.Text.Trim();
                    phongDuocChon.GhiChu = tb_ghiChu.Text.Trim();
                    
                    LoaiPhongDTO loaiPhongDuocChon = (LoaiPhongDTO)cbb_LoaiPhong.Items[cbb_LoaiPhong.SelectedIndex];
                    phongDuocChon.LoaiPhong.MaLoaiPhong=loaiPhongDuocChon.MaLoaiPhong;


                    bool ketQua = PhongBUS.capNhap(phongDuocChon);
                    if (ketQua == false)
                        throw new Exception();

                    MessageBox.Show("Cập nhật thành công !");
                    layDSPhong();
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Cập nhật thất bại !");
                }
            }
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            if (GridDSPhong.CurrentRow.Tag != null)
            {
                if (MessageBox.Show("Bạn có thật sự muốn xóa?", "Hỏi", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    PhongDTO phongDuocChon = (PhongDTO)GridDSPhong.CurrentRow.Tag;
                    bool ketQua = PhongBUS.xoa(phongDuocChon.MaPhong);
                    if (ketQua == false)
                    {
                        MessageBox.Show("Xóa thành công !");
                    }
                    else
                    {
                        MessageBox.Show("Xóa thất bại !");
                        layDSPhong();
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}
